package com.example.demo_upimg.upNimg;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping
public class UpNimg {
    @ResponseBody
    @RequestMapping(value = "/upnimg", method = {RequestMethod.GET, RequestMethod.POST})
    public void imageUploa(@RequestParam("file") MultipartFile[] file,
                           HttpServletRequest request, HttpServletResponse response, HttpSession session)
            throws IllegalStateException, IOException {
        System.out.println("收到一个上传多张图片的请求");
        List list = new ArrayList();
        // String images =  imageUtil.ImageUpload(file, request,response,session);
        if (file != null && file.length > 0) {
            //循环获取file数组中得文件
            for (int i = 0; i < file.length; i++) {
                MultipartFile files = file[i];
                //保存文件
                if (files.isEmpty()) {
                    return;
                }
                String fileName = files.getOriginalFilename();
                File dest = new File(new File("E:\\Zimg").getAbsolutePath() + "/" + i + fileName);
                if (!dest.getParentFile().exists()) {
                    dest.getParentFile().mkdirs();
                }
                try {
                    files.transferTo(dest); // 保存文件
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
